package com.javadroid.webservices;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MandandoJsonPost {
	
	
	//final String URL_TODOS_CAPITULOS = "http://192.168.25.8:8080/WebService_SmartNovelas/json/getNovelas/postone";
	//final String URL_TODOS_CAPITULOS = "http://smartnovelasapp.eu01.aws.af.cm/json/getNovelas/cargaDeDadosPorNovela";
	
	final String URL_TODOS_CAPITULOS = "http://lab_smartnovela.eu01.aws.af.cm/json/getNovelas/postone";
	//http://lab_smartnovela.eu01.aws.af.cm/json/getNovelas/postone
	
	private void openHttpConnectionGetJson(int id_novela) {

		
		StringBuilder sb = new StringBuilder();  

        HttpURLConnection urlConnection=null;  
        try {  
            URL url = new URL(URL_TODOS_CAPITULOS);  
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setDoOutput(true); 
            urlConnection.setDoInput(true);
            urlConnection.setRequestMethod("POST");  
            urlConnection.setUseCaches(false);  
            urlConnection.setConnectTimeout(10000);  
            urlConnection.setReadTimeout(10000);  
            urlConnection.setRequestProperty("Content-Type","application/json");   

            urlConnection.setRequestProperty("Host", "http://smartnovelasapp.eu01.aws.af.cm");
            //urlConnection.setRequestProperty("Host", "localhost:8080/WebService_SmartNovelas");
            urlConnection.connect();  

         
            String strjson =  "{\"id_nov\":1,\"max_cap\":0,\"max_nov\":12}"; 
            
            
            DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream ());
            wr.writeBytes(strjson);
            wr.flush();
            wr.close();
       

            int HttpResult = urlConnection.getResponseCode();  
            if(HttpResult == HttpURLConnection.HTTP_OK ||HttpResult == HttpURLConnection.HTTP_CREATED ){  
                BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"utf-8"));  
                String line = null;  
                while ((line = br.readLine()) != null) {  
                    sb.append(line + "\n");  
                }  
                br.close();  
                System.out.println(""+sb.toString());  
            }else{  
                System.out.println(urlConnection.getResponseMessage());  
            }  
        } catch (MalformedURLException e) {  
            e.printStackTrace();  
        }  
        catch (IOException e) {  
            e.printStackTrace();  
        } finally{  
            if(urlConnection!=null)  
              urlConnection.disconnect();  
        } 
	}
	
	public static void main(String[] args) {
		MandandoJsonPost p = new MandandoJsonPost();
		p.openHttpConnectionGetJson(1);
	}

}
